<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<style>
    label.change-style {
        position: relative;
        display: inline-block;
        text-align: center;
        line-height: 30px;
    }

    #upload-file2 {
        width: 50px;
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
    }

    ul {
        list-style-type: none;
    }

    li {
        margin: 15px 0;
    }

    img[src=""] {
        opacity: 0;
    }

    .change-style img {
        width: 100px;
        height: auto;
        vertical-align: top;
    }
</style>

<body>
    <div class="nav">
        <img src="image/image1.jpg" alt="">
        <span class="title">个人博客系统</span>
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout" style="display: none;">注销</a>
    </div>

    <div class="edit-container">
        <div class="edit-dialog">
            <h3>个人信息管理</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username" name="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password" name="password">
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" id="password2" name="password2">
            </div>
            <div class="row">
                <span>Git链接</span>
                <input type="gitlink" id="gitlink" name="gitlink">
            </div>
            <div class="row submit-row">
                <button id="submit" type="submit" value='确定' onclick="updateuser()">确定</button>
            </div>
            <div class='row submit-row'>
                <button id="submit" type="submit" value="重填" href='user_edit.html'>重填</button>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"> </script>
    <script>
        jQuery.ajax({
            url:"user/loginstate",
            type:"GET",
            success:function(result){
                if(result != null && result.success == 200){
                    if (result.data != null && result.data.userId > 0) {
                        jQuery('#username').val(result.data.username);
                        jQuery('#password').val(result.data.password);    
                        jQuery('#password2').val(result.data.password);
                        jQuery("#gitlink").val(result.data.github);
                    } else {
                        alert("请先登录！")
                        location.href = "login.html";
                    }
                }
            }
        })
        function updateuser() {
            var username = jQuery("#username");
            var password = jQuery("#password");
            var password2 = jQuery("#password2");
            var gitlink = jQuery("#gitlink");
            if (jQuery.trim(username.val()) == "") {
                alert("抱歉：请先输入登录名！");
                username.focus();
                return;
            }
            if (jQuery.trim(password.val()) == "") {
                alert("抱歉：请先输入密码！");
                password.focus();
                return;
            }
            if (jQuery.trim(password2.val()) == "") {
                alert("抱歉：请先输入确认密码！");
                password2.focus();
                return;
            }
            if (jQuery.trim(gitlink.val()) == "") {
                alert("抱歉：请先输入git链接！");
                password2.focus();
                return;
            }
            if (password.val().length < 8) {
                alert("抱歉：密码强度太低，密码位数必须大于等于8位！");
                password.focus();
                return;
            }
            if (password.val() != password2.val()) {
                alert("抱歉：两次输入的密码不一致，请检查！");
                password.focus();
                return;
            }
            jQuery.ajax({
                url: "user/update",
                type: "POST",
                data: {
                    "username": username.val(),
                    "password": password.val(),
                    "gitlink": gitlink.val()
                },
                success: function (result) {
                    if (result != null && result.success == 200) {
                        if (result.data = 1) {
                            alert("修改成功！");
                            location.href = "login.html";
                        } else {
                            alert("抱歉：修改失败，" + data.message);
                        }
                    } else {
                        alert("后台程序出错！");
                    }
                }
            })
        }
    </script>
</body>

</html>